package de.uni_saarland.coli.doclists.data.btree.manager;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import de.uni_saarland.coli.doclists.data.btree.INode;
import de.uni_saarland.coli.doclists.data.btree.SplitData;
import de.uni_saarland.coli.doclists.data.btree.nodes.BucketNode;
import de.uni_saarland.coli.doclists.data.btree.nodes.NonBucketNode;

/**
 * 
 * @author stefan
 * 
 *         Manages the nodes and values of a B+ Tree
 * @param <T>
 *            The type of data.
 */
public class MemoryBTreeNodeManager<T> implements IBTreeNodeManager<T>, Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6668206301005966527L;

	/**
	 * next node id;
	 */
	private int nextNode;

	/**
	 * next value id
	 */
	private int nextValueID;

	/**
	 * node map
	 */
	private Map<Integer, INode<T>> nodeMap;
	
	/**
	 * value map
	 */
	private Map<Integer, T> valueMap;

	/**
	 * Constructor.
	 */
	public MemoryBTreeNodeManager() {
		super();
		this.nextNode = 0;
		this.nextValueID = 0;

		this.nodeMap = new HashMap<Integer, INode<T>>();
		this.valueMap = new HashMap<Integer, T>();
	}

	@Override
	public INode<T> createNonBucketNode() {
		this.nextNode++;
		INode<T> node = new NonBucketNode<T>();
		node.setListIDX(this.nextNode);
		this.nodeMap.put(this.nextNode, node);
		return node;
	}

	@Override
	public INode<T> createNonBucketNode(SplitData<T> data) {
		this.nextNode++;
		INode<T> node = new NonBucketNode<T>(data);
		node.setListIDX(this.nextNode);
		this.nodeMap.put(this.nextNode, node);
		return node;
	}

	@Override
	public INode<T> createBucketNode() {
		this.nextNode++;
		INode<T> node = new BucketNode<T>();
		node.setListIDX(this.nextNode);
		this.nodeMap.put(this.nextNode, node);
		return node;
	}

	@Override
	public INode<T> getNode(int id) {
		if (this.nodeMap.containsKey(id))
			return this.nodeMap.get(id);
		else
			return null;
	}

	@Override
	public T getValue(int id) {
		if (this.valueMap.containsKey(id))
			return this.valueMap.get(id);
		else
			return null;
	}

	@Override
	public int addValue(T value) {
		this.nextValueID++;

		this.valueMap.put(this.nextValueID, value);

		return this.nextValueID;
	}

	@Override
	public void save(String path, String baseName) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void open(String path, String baseName) {
		// TODO Auto-generated method stub
		
	}

}
